﻿using System;
using System.Drawing;
using System.Windows.Forms;
using GMap.NET;

namespace LocationAwareness
{
    /// <summary>
    /// Class to display instances of type PoiDetail
    /// </summary>
    public partial class PoiDetailPanel : UserControl
    {
        private OSMMap map;
        private delegate void CloseMapControl();

        /// <summary>
        /// Creates instance of PoiDetailPanel.
        /// </summary>
        /// <param name="detail">PoiDetail instance to be displayed</param>
        /// <param name="latitude">Actual latitude value of the client</param>
        /// <param name="longtitude">Actual longtitude value of the client</param>
        public PoiDetailPanel(PoiDetail detail, double latitude, double longtitude)
        {
            InitializeComponent();

            lbName.Text = detail.Name;
            lbDistance.Text = detail.Distance.ToString();

            txtDetail.Text = detail.Name + Environment.NewLine + detail.StreetNr + Environment.NewLine
                + detail.Address + Environment.NewLine + Environment.NewLine + LocawaToken.TkOpening
                + ":" + Environment.NewLine + detail.Opening + Environment.NewLine + Environment.NewLine
                + LocawaToken.TkCategorie + ":" + Environment.NewLine + detail.Categorie
                + Environment.NewLine + Environment.NewLine + LocawaToken.TkDescription + ":"
                + Environment.NewLine + detail.Desc;

            pbDetailImage.Image = (Image)Functions.ScaleGraphics(detail.DetailImage, pbDetailImage.Width, pbDetailImage.Height);

            map = new OSMMap();
            tabMap.Controls.Add(map);
            map.updateMap(new PointLatLng(latitude, longtitude),
                          new PointLatLng(detail.Lat, detail.Long),
                          detail.Distance);
                                                                
        }

        /// <summary>
        /// Updates instance of PoiDetailPanel.
        /// </summary>
        /// <param name="detail">PoiDetail instance to be displayed</param>
        /// <param name="latitude">Actual latitude value of the client</param>
        /// <param name="longtitude">Actual longtitude value of the client</param>
        public void updatePoiDetailPanel(PoiDetail detail, double latitude, double longtitude)
        {
            map.updateMap(new PointLatLng(latitude, longtitude) ,new PointLatLng(detail.Lat, detail.Long),
                          detail.Distance);

            lbDistance.Text = detail.Distance.ToString();
        }

        public override DockStyle Dock
        {
            get
            {
                return base.Dock;
            }
            set
            {
                base.Dock = value;
            }
        }

        //private void closeMapControl()
        //{
        //    if (this.InvokeRequired)
        //        this.Invoke(new CloseMapControl(closeMapControl), new object[] { });
        //    else
        //    {
        //        map.Dispose();
        //    }
        //}
        
        /// <summary>
        /// Closes the GMapControl and disposes the PoiDetailPanel instance.
        /// </summary>
        public void closePoiDetailPanel()
        {
            //closeMapControl();
            map.Dispose();
            this.Dispose();
        }
    }
}
